package com.ctcemti.mdm.project.sys.controller;

import com.alibaba.fastjson.JSON;
import com.ctcemti.mdm.common.util.FileUploadUtils;
import com.ctcemti.mdm.framework.config.Global;
import com.ctcemti.mdm.framework.config.ServerConfig;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import sun.misc.BASE64Decoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.util.*;


/**
 * ueditor上传文件地址
 * <p>Title: UeditorController</p>
 * <p>Description: </p>
 * @author bxf
 * @date 2019年7月11日
 */
@Controller
@RequestMapping("/ueditor")
public class UeditorController {

	@Value("classpath:static/CTCE/core/ueditor/jsp/config.json")//自定义config.json位置
	private Resource areaRes;
	@Autowired
	private ServerConfig serverConfig;

	@RequestMapping(value="config")
	public void config(HttpServletRequest request, HttpServletResponse response,String action) throws IOException, URISyntaxException {
		response.setContentType("application/json");
		PrintWriter writer = response.getWriter();
		String uuid = UUID.randomUUID().toString();
		if("uploadscraw".equals(action)){
			String upfile = request.getParameter("upfile");
			boolean flag = generateImage(upfile,Global.getUploadPath()+"scraw",uuid+".jpg");
			if(flag){
				Map<String,Object> result = new HashMap<String,Object>();
				result.put("state", "SUCCESS");
				result.put("url", serverConfig.getUrl()+"/profile/upload/scraw/"+uuid+".jpg");
				result.put("title", "image.jpg");
				result.put("original", "image.jpg");
				System.err.println(result.toString()+"-----------"+ JSON.toJSONString(result));
				writer.write(JSON.toJSONString(result));
				writer.flush();
				writer.close();
			}
		}
		else if("config".equals(action)){
			try {
				String rPath = IOUtils.toString(areaRes.getInputStream(), Charset.forName("UTF-8"));//读取配置文件
				writer.write(rPath);
				writer.flush();
				writer.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}else{
			System.err.println(serverConfig.getUrl());
			List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
			boolean flag = FileUploadUtils.springUpload(request, Global.getUploadPath(),uuid,list);
			if(flag){
				Map<String,Object> result = list.get(0);
				result.put("state", "SUCCESS");
				result.put("url", serverConfig.getUrl()+"/profile/upload/"+String.valueOf(result.get("newfilename")));
				result.put("title", String.valueOf(result.get("newfile")));
				result.put("original", String.valueOf(result.get("newfile")));
				System.err.println(result.toString()+"-----------"+ JSON.toJSONString(result));
				writer.write(JSON.toJSONString(result));
				writer.flush();
				writer.close();
			}
		}

	}

	//base64字符串转化成图片
	public boolean generateImage(String imgStr, String path,String filename)
	{   //对字节数组字符串进行Base64解码并生成图片
		if (imgStr == null) //图像数据为空
			return false;
		BASE64Decoder decoder = new BASE64Decoder();
		try
		{
			//Base64解码
			byte[] b = decoder.decodeBuffer(imgStr);
			for(int i=0;i<b.length;++i)
			{
				if(b[i]<0)
				{//调整异常数据
					b[i]+=256;
				}
			}
			//生成jpeg图片
			//String imgFilePath = "D:\\tupian\\new.jpg";//新生成的图片
			OutputStream out = new FileOutputStream(path+"/"+filename);
			out.write(b);
			out.flush();
			out.close();
			return true;
		}
		catch (Exception e)
		{
			return false;
		}
	}

}
